<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>



    <script>
        function getFoo() {                                     //返回一个promise对象这里假设是异步操作
            return new Promise(function (resolve, reject) {
                resolve('foo');
            });
        }
        var g = function* () {

            try {
                var foo = yield getFoo();
                // console.log(foo);
                var foo = yield getFoo();
             
            } catch (e) {
                console.log(e);
            }

        };

        function run(generator) {                           //把生成器传进来
            var it = generator();

            function go(result) {
                if (result.done) return result.value;       //判断生成器是否到头

                return result.value.then(function (value) {

                    console.log(value)

                    return go(it.next(value));
                }, function (error) {
                    return go(it.throw(error));
                });
            }

            go(it.next());
        }
            
        run(g);
        
    </script>
</body>

</html>